<template> 
  <div class="app-container">
    <!--隐藏或展开筛选条件-->
    

     <!--筛选条件-->
    <el-card  class="filter-container" shadow="never" >
      <el-row>
          
            <el-col :span="12">
              <el-button
                style="float: right"
                @click="handleUpdateExam()"
                type="primary"
                size="small">
                修改
              </el-button>
             
             
            </el-col>
            <el-col :span="4">
              <el-button
                style="float: right"
                @click="handlePrint(0)"
                type="primary"
                size="small">
                打印试卷
              </el-button>
            </el-col>
            <el-col :span="4">
              <el-button
                style="float: right"
                @click="handlePrint(1)"
                type="primary"
                size="small">
                打印答案
              </el-button>
            </el-col>

      </el-row>
       <el-row>
          <el-col :span="24">
           <el-form :inline="true" label-position="right" :model="examInfo"  ref="examInfoRef" :rules="examInfoRule" size="mini" label-width="140px">
             
              <el-form-item label="卷子状态：">
                <el-select v-model="examInfo.status" placeholder="请选择试卷状态" clearable>
                <el-option
                    v-for="item in examStatusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                 </el-select>
              </el-form-item>
              <el-form-item label="卷子名称：" size="small" prop="name" >
                <el-input  v-model="examInfo.name"  placeholder="请输入"></el-input>
              </el-form-item>
               <el-form-item label="卷子说明：" >
                <el-input  v-model="examInfo.comment"  placeholder="请输入" type="textarea" autosize ></el-input>
              </el-form-item>
             

            </el-form>
          </el-col>
      </el-row>
    </el-card>
    
    <div class="table-container">
      <el-table ref="productTable"
                :data="list"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                v-loading="listLoading"
                border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="题号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="题目说明" width="320" align="center">
          <template slot-scope="scope">{{scope.row.comment}}</template>
        </el-table-column>
      
        <el-table-column label="题型" width="140" align="center">
          <template slot-scope="scope">{{scope.row.typeId| optionFilter(allTypeOptions)}}</template>
        </el-table-column>
        <el-table-column label="难度" width="100" align="center">
          <template slot-scope="scope">{{scope.row.difStatus| optionFilter(difStatusOptions)}}</template>
        </el-table-column>
        <el-table-column label="推荐程度" width="100" align="center">
          <template slot-scope="scope">{{scope.row.starStatus | optionFilter(starStatusOptions)}}</template>
        </el-table-column>
        

        <el-table-column label="做题情况" width="100" align="center" >
          <template slot-scope="scope">{{scope.row.testStatus| optionFilter(testStatusOptions)}}</template>
        </el-table-column>
          <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <p>
              

              <el-button 
                size="mini" type="primary"
                @click="showUpdateTestStatusDialog(scope.$index, scope.row)">批改
              </el-button>
            </p>
            <p>
              <el-button 
                size="mini" 
                @click="handleShowQuestion(scope.$index, scope.row)">查看标准答案
              </el-button>
            </p> 
             <el-button 
                size="mini" type="warning"
                @click="handleRemoveQuestion(scope.$index, scope.row)">删除
              </el-button>
            </p>
           
            
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,20,30]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
    
      

    
  </div>
</template>
<script>
  import { mapGetters } from 'vuex'
  import { fetchList } from '@/api/question';
  import {removeQuestionFromExam,updateExam ,fetchQuestionList} from '@/api/exam';
  
  import {deepClone} from '@/utils/helper';

  const defaultListQuery = {
   
    pageNum: 1,
    pageSize: 3,
    
    subjectId: null,    //学科Id
    status:null,
    name:null,
    comment:null

  };
  export default {
    name: "examView",
    data() {
       

    
      return {
       
        


        listQuery: {
            pageNum: 1,
            pageSize: 10,
            questionIds:null
        },
        examInfo: {
          id: this.$route.query.id,
          questionIds: this.$route.query.questionIds,
          name: this.$route.query.name,
          status: parseInt(this.$route.query.status),
          comment: this.$route.query.comment,
        },

        list: null,
        total: null,
        listLoading: false,
       

        
        //自定义listQueryForm的校验规则

        examInfoRule :{
            name: [
             {required: true, message:"请输入卷子名称",trigger: ['blur','change']},
          ],
        },

       
       
        
       
      }
    },
    computed:{
      ...mapGetters([
          'name',
          'subjectOptions',
          'examStatusOptions',
          'userid',
          'allTypeOptions',
          'starStatusOptions',
          'testStatusOptions',
          'difStatusOptions',
        ]),
      
    },
    created(){
      this.getQuestionList(this.examInfo.id)
     
    },
   
    filters: {
      optionFilter(value,options) {
        let res= options.find(function(x) {
          return x.value ===value;
          }
        )
        if (res){
          return res.label
        }else{
          return ''
        }
      },
    },
 
    methods: {

      handleShowQuestion(index,row){
        let routeData = this.$router.resolve({path:'/qms/questionView',query:{id:row.id}});
        window.open(routeData.href, '_blank');

        this.$router.push({path:'/qms/questionView',query:{id:row.id}});
      },
      showUpdateTestStatusDialog(index,row){
        let routeData = this.$router.resolve({path:'/qms/testStatusUpdate',query:{id:row.id,testStatus:row.status}});
        window.open(routeData.href, '_blank');

        //this.$router.push({path:'/qms/testStatusUpdate',query:{id:row.id,testStatus:row.testStatus}});
      },
      handlePrint(bAnswer){
        let routeData = this.$router.resolve({path:'/ems/examPrint',query:{bAnswer:bAnswer,id:this.examInfo.id,name:this.examInfo.name,questionIds:this.examInfo.questionIds}});
        window.open(routeData.href, '_blank');
      },

      handleUpdateExam(){

        this.$refs.examInfoRef.validate(valid => {
          if (valid) {

               let params={}
              
               
                params['name']=this.examInfo.name
                params['comment']=this.examInfo.comment
                params['status']=this.examInfo.status
                updateExam(this.examInfo.id,params).then(response=>{
                  this.$message({
                                  type: 'success',
                                  message: '更新成功',
                                  duration:1000
                              });

                  
                
                 })


          }
        })
        

      },
      
      handleViewExam(index,row){
        let routeData = this.$router.resolve({path:'/ems/view',query:{id:row.id}});
        window.open(routeData.href, '_blank');

        //this.$router.push({path:'/qms/questionUpdate',query:{id:row.id}});
      },
     
      getQuestionList(examId){
          fetchQuestionList({"examId":examId}).then(response=>{

                   this.listQuery.questionIds=response.data.questionIds
                   this.getList()
                  
                }).catch(error => {

                      this.$message({
                          type: 'error',
                          message: '获取试题失败',
                          duration:1000
                      });
              
                  });


      },
      handleRemoveQuestion(index,row){
        
        this.$confirm('确认从试卷中删除该试题？', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  console.log("to remove")
                  let params={}
                  params['questionId']=row.id+""
                  params['examId']=this.examInfo.id
                  console.log(params)
                  removeQuestionFromExam(params).then(response=>{

                        this.$message({
                              type: 'success',
                              message: '删除成功',
                              duration:1000
                          });

                       this.getQuestionList(this.examInfo.id)

                  
                }).catch(error => {

                      this.$message({
                          type: 'error',
                          message: '删除失败',
                          duration:1000
                      });
              
                  });


                }).catch(() => {
                      
                    console.log("err")
                 });

      },

    
     


     


      //获得试题列表
      getList() {
        

              this.listLoading = true;

              var submitParam=deepClone(this.listQuery)
              
              
              submitParam.userId=this.userid
              

              fetchList(submitParam).then(response => {
                this.listLoading = false;
                this.list = response.data.list;
                this.total = response.data.total;
              });
          
    },

     


      //点击“查询结果按钮”
      handleSearchList() {
        this.$refs.listQueryRef.validate(valid => {
          if (valid) {
            this.listQuery.pageNum = 1;
            this.getList();
          }
        })
      },
     
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      
     
    },
   
  }
</script>



